package com.zmp.kotlinnote.utils

import android.content.Context
import android.graphics.Color
import android.util.Log
import com.zmp.kotlinnote.view.SvgPathHelper
import org.dom4j.Element
import org.dom4j.io.SAXReader

/**
 * @author zmp
 * Created by zmp on 2018/12/10
 */
class XmlParseTools {
    companion object {
        private const val TAG = "XmlParseTools"

        fun parseSvg(context: Context, rawRes: Int): List<SvgPathData> {
            val saxReader = SAXReader()
            val rawResource = context.resources.openRawResource(rawRes)
            val document = saxReader.read(rawResource)
            val rootElement = document.rootElement
            Log.e(TAG, "rootElement.name:${rootElement.name}")
            val listOf = arrayListOf<SvgPathData>()
            addPath(rootElement, listOf)
            val elementsG = rootElement.elements("g")
            Log.e(TAG, "elements.name:${elementsG.size}")
            elementsG?.forEach {
                addPath(it as Element, listOf)
            }
            return listOf
        }

        private fun addPath(
            rootElement: Element,
            listOf: ArrayList<SvgPathData>
        ) {
            val elements = rootElement.elements("path")
            Log.e(TAG, "elements.name:${elements.size}")
            elements.forEach {
                val element: Element = it as Element
                val fillColor = element.attribute("fill")
                val strokeColor = element.attribute("stroke")
                val pathData = element.attribute("d")
                val parser = SvgPathHelper().parserSvgPath(pathData.value)
                val svgPathData = SvgPathData(parser)
                if (fillColor != null) {
                    svgPathData.fillColor = Color.parseColor(fillColor.value)
                }
                if (strokeColor != null) {
                    svgPathData.strokeColor = Color.parseColor(strokeColor.value)
                }
                Log.e(TAG, "rootElement.svgPathData:$svgPathData}")
                listOf.add(svgPathData)
            }
        }

        fun parseSvgVector(context: Context, rawRes: Int): List<SvgPathData> {
            val saxReader = SAXReader()
            val rawResource = context.resources.openRawResource(rawRes)
            val document = saxReader.read(rawResource)
            val rootElement = document.rootElement
            Log.e(TAG, "rootElement.name:${rootElement.name}")
            val elements = rootElement.elements("path")
            val listOf = arrayListOf<SvgPathData>()
            elements.forEach {
                val element: Element = it as Element
                val fillColor = element.attribute("fillColor")
                val strokeColor = element.attribute("strokeColor")
                val pathData = element.attribute("pathData")
                val parser = SvgPathHelper().parserSvgPath(pathData.value)
                val svgPathData = SvgPathData(parser)
                if (fillColor != null) {
                    svgPathData.fillColor = Color.parseColor(fillColor.value)
                }
                if (strokeColor != null) {
                    svgPathData.strokeColor = Color.parseColor(strokeColor.value)
                }
                listOf.add(svgPathData)
            }
            return listOf
        }
    }
}
